home *** CD-ROM | disk | FTP | other *** search
/ Zoom 2 / Zoom - Release 2 (1996)(Active Software)[!].iso / graphics / misc / opticon18 / opticon.rexx < prev    next >
OS/2 REXX Batch file  |  1978-06-28  |  8KB  |  309 lines

  1. /*
  2.  *  $VER: $Id: opticon.rexx,v 1.3 1994/07/17 22:41:17 tf Exp $
  3.  */
  4.  
  5. /*
  6.  * (c)Copyright 1994 by Tobias Ferber
  7.  *
  8.  * This file is part of the Icon2C and OptIcon distribution.
  9.  *
  10.  * Icon2C and OptIcon are free software; you can redistribute them and/or
  11.  * modify them under the terms of the GNU General Public License as published
  12.  * by the Free Software Foundation; either version 1 of the License,
  13.  * or (at your option) any later version.
  14.  *
  15.  * Icon2C and Opticon are distributed in the hope that they will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with these programs; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. OPTIONS FAILAT 10
  26.  
  27. pathname = ""
  28. destpath = ""
  29. planes   = 0
  30. pattern  = "#?.info"
  31. tempfile = "T:OptIconTemp." || pragma('Id')
  32. template = "FROM/K/A,TO/K,ALL/S,PAT/K,DEPTH=PLANES/K/A,NOEXPAND/S"
  33. args     = ""
  34. cliopts  = ""
  35. optiargs = ""
  36. lsargs   = ""
  37.  
  38. /* parse args */
  39.  
  40. IF ( ARG() < 1 ) | ( (ARG() = 1) & ARG(1)= '?' )  THEN DO
  41.   OPTIONS PROMPT template': '
  42.   PARSE PULL args
  43.   END
  44. ELSE DO n=1 FOR ARG() /* RXFB_TOKEN for RX ?! */
  45.   ARGS= ARGS || ARG(n)
  46.   END
  47.  
  48. DO WHILE WORDS(args) > 0
  49.   av= next_arg()
  50.   SELECT
  51.  
  52.     /* script args */
  53.  
  54.     WHEN UPPER(av) = "FROM" THEN DO
  55.       IF WORDS(args) > 0 THEN DO
  56.         pathname= next_arg()
  57.         IF WORDS(pathname) < 1 THEN pathname= PRAGMA('D')
  58.         END
  59.       ELSE EXIT bad_args("Missing pathname after FROM keyword")
  60.       END /* FROM */
  61.  
  62.     WHEN UPPER(av) = "TO" THEN DO
  63.       IF WORDS(args) > 0 THEN DO
  64.         destpath= next_arg()
  65.         IF WORDS(destpath) < 1 THEN destpath= PARGMA('D')
  66.         END
  67.       ELSE EXIT bad_args("Missing pathname after TO keyword")
  68.       END /* TO */
  69.  
  70.     /* OptIcon args */
  71.  
  72.     WHEN (UPPER(av) = "DEPTH") | (UPPER(av) = "PLANES") THEN DO
  73.       IF WORDS(args) > 0 THEN planes= next_arg()
  74.       ELSE EXIT bad_args("Missing #of bitplanes " UPPER(av) "keyword")
  75.       IF (LENGTH(planes) > 1) | (LENGTH(COMPRESS(planes,"12345678")) > 0) THEN
  76.         EXIT bad_args("Illegal #of bitplanes:" planes "Should be one of 1,2,...,8.")
  77.       END /* DEPTH=PLANES */
  78.  
  79.     WHEN UPPER(av) = "NOEXPAND" then optiargs = "NOEXPAND"
  80.  
  81.     /* List args */
  82.  
  83.     WHEN UPPER(av) = "ALL" THEN DO
  84.       IF POS("ALL",lsopts) < 1 THEN lsargs = lsargs || " ALL"
  85.       END /* ALL */
  86.  
  87.     WHEN UPPER(av) = "PAT" THEN DO
  88.       IF WORDS(args) > 0 THEN pattern= next_arg()
  89.       ELSE EXIT bad_args("Missing pattern after PAT keyword")
  90.       END /* PAT */
  91.  
  92.     /* illegal args */
  93.  
  94.     OTHERWISE DO
  95.       IF av ~= '?' THEN EXIT bad_args("Unknown keyword" av)
  96.                    ELSE EXIT bad_args("")
  97.       END
  98.  
  99.   END /* SELECT */
  100.  
  101. END /* DO */
  102.  
  103. IF planes = 0 THEN EXIT bad_args("Missing #of bitplanes for DEPTH=PLANES/K/A")
  104.  
  105. CALL PRAGMA('W','N')
  106.  
  107. /* try to get missing pathname */
  108.  
  109. IF (WORDS(pathname) < 1) & (EXISTS('c:RequestFile')) THEN DO
  110.   cwd= PRAGMA('D')
  111.   ADDRESS COMMAND 'RequestFile >' tempfile 'DRAWER "'cwd'" TITLE "Select a path..." DRAWERSONLY NOICONS'
  112.  
  113.   IF OPEN('fp',tempfile,'R') THEN DO
  114.     pathname= STRIP(READLN('fp'),'B','"')
  115.     CALL CLOSE('fp')
  116.     ADDRESS COMMAND 'Delete QUIET FILE' tempfile
  117.     END
  118.   ELSE pathname= ""
  119.   END
  120.  
  121. IF WORDS(pathname) < 1 THEN EXIT bad_args("missing FROM pathname")
  122.  
  123. IF ~EXISTS(pathname) THEN DO
  124.   SAY 'Failed to locate your FROM path "'pathname'"'
  125.   EXIT 10
  126.   END
  127.  
  128. /**/
  129.  
  130. IF ~canexist(destpath) THEN DO
  131.   SAY 'Illegal destination directory "'destpath'"'
  132.   EXIT 10
  133.   END
  134.  
  135. /**/
  136.  
  137. SAY 'Collecting icons ...  Please wait ...'
  138.  
  139. cwd= PRAGMA('D',pathname)
  140. ADDRESS COMMAND 'List FILES PAT' pattern 'LFORMAT "%p%n"' lsargs 'TO "'tempfile'"'
  141. CALL PRAGMA('D',cwd)
  142.  
  143. SIGNAL ON HALT
  144. SIGNAL ON BREAK_C
  145. SIGNAL ON BREAK_D
  146.  
  147. IF ~OPEN('fp',tempfile,'R') THEN DO
  148.   SAY 'Error: could not open temporary file "'tempfile'"'
  149.   EXIT 10
  150.   END
  151.  
  152. DO UNTIL EOF('fp')
  153.   fname= STRIP( READLN('fp') )
  154.   IF WORDS(fname) > 0 THEN DO
  155.     fromfile= tackon(pathname,fname)
  156.  
  157.     IF WORDS(destpath) > 0 THEN DO
  158.       pname= tackon(destpath,pathonly(fname))
  159.  
  160.       IF ~EXISTS(pname) & canexist(pname) THEN DO
  161.         IF POS('m',cliopts) > 0 THEN CALL makepath(pname)
  162.         ELSE DO
  163.           OPTIONS PROMPT 'Destination path "'pname'" does not exist.  Shall I create it? (Y/n/a) '
  164.           PULL yna
  165.           IF LEFT(yna,1) ~= 'N' THEN DO
  166.             CALL makepath(pname)
  167.             IF LEFT(yna,1) = 'A' THEN cliopts = cliopts || 'm'
  168.             END
  169.           END
  170.         IF EXISTS(pname) THEN SAY pname '  [created]'
  171.         END
  172.  
  173.       IF EXISTS(pname) THEN DO
  174.         iconfile= tackon(destpath,fname)
  175.         /*SAY 'Copying' fname 'TO' iconfile*/
  176.         ADDRESS COMMAND 'Copy QUIET FROM' transquote(fromfile) 'TO' transquote(iconfile)
  177.         END
  178.  
  179.       ELSE DO
  180.         SAY 'No such directory "'pname'" ... ' fileonly(fname) 'skipped.'
  181.         iconfile= ""
  182.         END
  183.  
  184.       END
  185.     ELSE iconfile= fromfile
  186.  
  187.     IF WORDS(iconfile) > 0 THEN DO
  188.       SAY '  ' iconfile
  189.       ADDRESS COMMAND 'OptIcon NAME' transquote(iconfile) 'PLANES' planes optiargs
  190.       END
  191.  
  192.     END
  193.  
  194.   END /* DO */
  195.  
  196. CALL CLOSE('fp')
  197. ADDRESS COMMAND 'DELETE QUIET FILE "'tempfile'"'
  198. SAY 'done.'
  199. EXIT
  200.  
  201. /**/
  202.  
  203. bad_args: PROCEDURE EXPOSE template
  204.   PARSE ARG str
  205.   IF WORDS(str) > 0 THEN SAY str
  206.   SAY "Template:" template
  207.   SAY "Usage: rx Opticon.rexx FROM <pathname> [TO <destpath>] [ALL] [PAT <pattern>] PLANES [1..8] [NOEXPAND]"
  208.   RETURN 10
  209.  
  210. /*@*/
  211.  
  212. /* get the next command-line argument from global 'args' string */
  213.  
  214. next_arg: PROCEDURE EXPOSE args
  215.   args= STRIP(args)
  216.   IF LEFT(args,1) = '"' THEN PARSE VAR args '"' a '"' args
  217.                         ELSE PARSE VAR args     a     args
  218.   RETURN STRIP(a,'b','"');
  219.  
  220.  
  221. /* translate '"' into '*"' and '*' into '**' */
  222.  
  223. transquote: PROCEDURE
  224.   PARSE ARG s
  225.   t= s
  226.   q= MAX( LASTPOS('*',s), LASTPOS('"',s) )
  227.   DO WHILE q > 0
  228.     t= INSERT('*',t,q-1,1)
  229.     s= LEFT(s,q-1)
  230.     q= MAX( LASTPOS('*',s), LASTPOS('"',s) )
  231.     END
  232.   RETURN '"' || t || '"'
  233.  
  234.  
  235. /* return the non-file part of a pathname */
  236.  
  237. pathonly: PROCEDURE
  238.   PARSE ARG path
  239.   IF (WORDS(path) > 0) & (RIGHT(path,1) ~= ':') THEN DO
  240.     IF RIGHT(path,1) = '/' THEN path= LEFT(path,LENGTH(path)-1)
  241.     IF LASTPOS('/',path) > LASTPOS(':',path) THEN path= LEFT(path,LASTPOS('/',path)-1)
  242.                                              ELSE path= LEFT(path,LASTPOS(':',path))
  243.     END
  244.   RETURN path
  245.  
  246.  
  247. /* return the file part of a pathname */
  248.  
  249. fileonly: PROCEDURE
  250.   PARSE ARG path
  251.   IF RIGHT(path,1) = '/' THEN PATH= LEFT(path,LENGTH(path)-1)
  252.   p= MAX( LASTPOS(':',path), LASTPOS('/',path) )
  253.   IF(p>0) THEN RETURN substr(path,p+1)
  254.           ELSE RETURN path
  255.  
  256.  
  257. /* concatenate the filename to the pathname and return the resulting string */
  258.  
  259. tackon: PROCEDURE
  260.   PARSE ARG path,file
  261.   DO WHILE LEFT(file,1) = '/'
  262.     file= SUBSTR(file,2)
  263.     path= pathonly(path)
  264.     END
  265.   IF (WORDS(path) > 0) & (RIGHT(path,1) ~= '/') & (RIGHT(path,1) ~= ':') THEN path= path || '/'
  266.   IF (RIGHT(file,1) = '/') THEN file= LEFT(file,LENGTH(file)-1)
  267.   RETURN path || file
  268.  
  269.  
  270. /* create all non-existant directories in a path */
  271.  
  272. makepath: PROCEDURE
  273.   PARSE ARG path
  274.   IF RIGHT(path,1) = '/' THEN path= LEFT(path,LENGTH(path)-1)
  275.   IF ~EXISTS(path) THEN DO
  276.     CALL makepath( pathonly(path) )
  277.     ADDRESS COMMAND 'MakeDir NAME "'path'"'
  278.     END
  279.   RETURN 0
  280.  
  281.  
  282. /*
  283.  * return   1  if the device or volume name in given pathname exists
  284.  *             or if no device or volume was present (current device)
  285.  *          0  if the device or volume name does not exist
  286.  */
  287.  
  288. canexist: PROCEDURE
  289.   PARSE UPPER ARG path
  290.   IF POS(':',path) < 1 THEN RETURN 1 /* current device */
  291.   CALL PRAGMA('W','N')
  292.   RETURN EXISTS( LEFT(path,LASTPOS(':',path)) )
  293.  
  294.  
  295. /* break traps */
  296.  
  297. HALT:
  298. BREAK_C:
  299. BREAK_D:
  300.   SIGNAL OFF HALT
  301.   SIGNAL OFF BREAK_C
  302.   SIGNAL OFF BREAK_D
  303.  
  304.   SAY 'Execution halted.'
  305.   EXIT
  306.  
  307.  
  308. /* EOF */
  309.